const isConcatSpreadable = require('core-js/fn/symbol/is-concat-spreadable');
var express = require('express');
var router = express.Router();
// 加载数据库
var connection = require('../db/sql.js')

var QcloudSms = require("qcloudsms_js");

let jwt = require('jsonwebtoken');
// const { createLogger } = require('vuex');
// 引入支付宝配置文件
const alipaySdk = require('../db/alipay.js');
const AlipayFormData = require('alipay-sdk/lib/form').default;
const axios = require('axios');




function getTimeToken(exp) {

	let getTime = parseInt(new Date().getTime() / 1000);

	if (getTime - exp > 60) {
		return true;
	}

}

/* GET home page. */
router.get('/', function (req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

// //支付状态
router.post('/api/successPayment', function (req, res, next) {
	//token
	let token = req.headers.token;
	let tokenObj = jwt.decode(token);
	//订单号
	let out_trade_no = req.body.out_trade_no;
	let trade_no = req.body.trade_no;
	//支付宝配置
	console.log(out_trade_no, trade_no);
	const formData = new AlipayFormData();
	// 调用 setMethod 并传入 get，会返回可以跳转到支付页面的 url
	formData.setMethod('get');
	//支付时信息
	formData.addField('bizContent', {
		out_trade_no,
		trade_no
	});
	//返回promise
	const result = alipaySdk.exec(
		'alipay.trade.query',
		{},
		{ formData: formData },
	);

	//后端请求支付宝
	result.then(resData => {
		axios({
			method: 'GET',
			url: resData
		}).then(data => {
			let responseCode = data.data.alipay_trade_query_response;
			if (responseCode.code == '10000') {
				switch (responseCode.trade_status) {
					case 'WAIT_BUYER_PAY':
						res.send({
							data: {
								code: 0,
								data: {
									msg: '支付宝有交易记录，没付款'
								}
							}
						})
						break;

					case 'TRADE_CLOSED':
						res.send({
							data: {
								code: 1,
								data: {
									msg: '交易关闭'
								}
							}
						})
						break;

					case 'TRADE_FINISHED':
						connection.query(`select * from user_list where tel = ${tokenObj.tel}`, function (error, results) {
							//用户id
							let uId = results[0].id;
							connection.query(`select * from order_list where uid = ${uId} and order_id = ${out_trade_no}`, function (err, result) {
								let id = result[0].id;
								//订单的状态修改掉2==》3
								connection.query(`update order_list set order_status = replace(order_status,'2','3') where id = ${id}`, function () {
									res.send({
										data: {
											code: 2,
											data: {
												msg: '交易完成'
											}
										}
									})
								})
							})
						})
						break;

					case 'TRADE_SUCCESS':
						connection.query(`select * from user_list where tel = ${tokenObj.tel}`, function (error, results) {
							//用户id
							let uId = results[0].id;
							connection.query(`select * from order_list where uid = ${uId} and order_id = ${out_trade_no}`, function (err, result) {
								let id = result[0].id;
								//订单的状态修改掉2==》3
								connection.query(`update order_list set order_status = replace(order_status,'2','3') where id = ${id}`, function () {
									res.send({
										data: {
											code: 2,
											data: {
												msg: '交易完成'
											}
										}
									})
								})
							})
						})
						break;
				}
			} else if (responseCode.code == '40004') {
				res.send({
					data: {
						code: 4,
						msg: '交易不存在'
					}
				})
			}
		}).catch(err => {
			res.send({
				data: {
					code: 500,
					msg: '交易失败',
					err
				}
			})
		})
	})
})


//发起支付
router.post('/api/payment', function (req, res, next) {
	//订单号
	let orderId = req.body.orderId;
	//商品总价
	let price = req.body.price;
	//购买商品的名称
	let name = req.body.name;

	//开始对接支付宝API
	const formData = new AlipayFormData();
	// // 调用 setMethod 并传入 get，会返回可以跳转到支付页面的 url
	formData.setMethod('get');
	// //支付时信息
	formData.addField('bizContent', {
		outTradeNo: orderId,//订单号
		productCode: 'FAST_INSTANT_TRADE_PAY',//写死的
		totalAmount: price,//价格
		subject: name,//商品名称
	});

	// //支付成功或者失败跳转的链接
	formData.addField('returnUrl', 'http://localhost:8080/payment');
	console.log(111);
	// //返回promise
	const result = alipaySdk.exec(
		'alipay.trade.page.pay',
		{},
		{ formData: formData },
	);

	// //对接支付宝成功，支付宝方返回的数据
	result.then(resp => {
		res.send({
			data: {
				code: 200,
				success: true,
				msg: '支付中',
				paymentUrl: resp
			}
		})
	})
})

// 修改订单状态
router.post('/api/submitOrder', (req, res) => {
	console.log(req.body.order_id);
	// console.log(req.body.shopArr);
	let orderId = req.body.order_id;
	let shopArr = req.body.shopArr;
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		let uid = results[0].id;
		connection.query(`update order_list set order_status = "2" where uid = "${uid}" and order_id = "${orderId}"`, (err, results) => {
			if (err) { throw err }
			// 数据删除
			shopArr.forEach(v => {
				connection.query(`delete from goods_cart where id = ${v}`, (err, results) => {
					if (err) { throw err }
				})
			})
			res.send({
				code: 200,
				msg: '修改成功',
				success: true
			})
		})
	})
})

// 查找order数据
router.post('/api/selectOrder', (req, res) => {
	let orderId = req.body.orderId;
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;

	connection.query(`select * from order_list where order_id = ${orderId}`, (err, results) => {
		if (err) {
			throw err
		}

		res.send({
			code: 200,
			success: true,
			data: results
		})
	})
	// console.log(orderId);
})

// 生成订单
router.post('/api/addOrder', (req, res) => {
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	let data = req.body.arr;
	console.log(data);
	// 生成订单的函数   时间加四位随机数
	function setTimeDate() {
		let dt = new Date()
		const yyyy = dt.getFullYear()
		const MM = (dt.getMonth() + 1 + '').padStart(2, '0')
		const dd = (dt.getDate() + '').padStart(2, '0')
		const hh = (dt.getHours() + '').padStart(2, '0')
		const mm = (dt.getMinutes() + '').padStart(2, '0')
		const ss = (dt.getSeconds() + '').padStart(2, '0')
		let randomNum = Math.round(Math.random() * 100000);
		return yyyy + MM + dd + hh + mm + ss + randomNum;
	}
	// console.log(setTimeDate());
	let order_id = setTimeDate();
	let goods_name = []
	let goods_price = 0;
	let goodsNum = 0;
	console.log(data);
	data.forEach(item => {
		goods_name.push(item.goods_name)
		goods_price += item.goods_price * item.goods_num;
		goodsNum += item.goods_num - 0;
	});
	console.log(order_id, goods_name, goods_price, goodsNum);
	// 查用户ID
	connection.query(`select * from user_list where tel = ${tel}`, (err, results) => {
		if (err) { throw err }
		let uid = results[0].id;
		// 开始插入
		connection.query(`insert into order_list (uid,order_id,goods_name,goods_price,goods_num,order_status) values(${uid},"${order_id}","${goods_name}","${goods_price}",${goodsNum},"1")`, (err, r) => {
			if (err) { throw err }
			// 添加成功返回订单号给前端、
			connection.query(`select * from order_list where uid = ${uid} and order_status = '1' and order_id = ${order_id}`, (err, results) => {
				res.send({
					code: 200,
					success: true,
					data: results
				})
			})
		})
	})
})

// 修改购物车数量
router.post('/api/updateNum', (req, res) => {
	let id = req.body.id;
	let num = req.body.num;
	connection.query(`update goods_cart set goods_num = "${num}" where id = "${id}"`, (err, results) => {
		if (err) { throw err }
		res.send({
			code: 200,
			success: true,
			msg: "更新成功"
		})
	})
})

// 删除购物车数据
router.post('/api/deleteCart', (req, res) => {
	console.log(req.body.arrId);
	let arrId = req.body.arrId;
	for (let i = 0; i < arrId.length; i++) {
		connection.query(`delete from goods_cart where id = ${arrId[i]}`, (err, results) => {
			if (err) { throw err }
		})
	}
	res.send({
		code: 0,
		msg: '删除成功',
		success: true
	})
})

// 获取用户对应的收货地址
router.get('/api/addressData', (req, res) => {
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		if (err) throw err;
		// console.log(results);
		let uid = results[0].id
		connection.query(`select * from address where uid = "${uid}"`, (err, resultss) => {
			if (err) throw err;
			console.log(resultss);
			res.send({
				code: 0,
				success: true,
				data: {
					resultss
				}
			})
		})

	})
})

// 删除收货地址
router.post('/api/deleteAddress', (req, res) => {
	let id = req.body.id;
	let token = req.headers.token;
	let user = jwt.decode(token)
	let tel = user.tel;
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		let uid = results[0].id;
		connection.query(`delete from address where uid = ${uid} and id = ${id}`, (err, results) => {
			if (err) { throw err }
			res.send({
				code: 200,
				msg: "删除成功",
				success: true
			})
		})
	})
})

// 修改收货地址
router.post('/api/updateAddress', (req, res) => {
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	let data = req.body;
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		if (err) throw err;
		// console.log(results);
		let uid = results[0].id;
		let id = data.content.id;
		// 根据uid存收货地址
		let name = data.content.name;
		let tel = data.content.tel;
		let province = data.content.province;
		let city = data.content.city;
		let county = data.content.county;
		let addressDetail = data.content.addressDetail;
		let isDefault = data.content.isDefault;
		let areaCode = data.content.areaCode;
		// 如果isDefault == 1,让address中uid相同的isDefault设置为0
		if (isDefault == 1) {
			connection.query(`update address set isDefault = 0 where uid = ${uid}`, (err, results) => {
				if (err) { return err }
				// 然后再修改
				connection.query(`update address set name = ?,tel = ?,province=?,city=?,county =?,addressDetail = ?,isDefault = ?,areaCode = ? where id = ${id} and uid = ${uid}`,
					[name, tel, province, city, county, addressDetail, isDefault, areaCode], (err, results) => {
						// console.log(results);
						if (err) { throw err }
						res.send({
							code: 200,
							msg: '修改成功',
							success: true
						})
					})
			})
		} else {
			connection.query(`update address set name = ?,tel = ?,province=?,city=?,county =?,addressDetail = ?,isDefault = ?,areaCode = ? where id = ${id} and uid = ${uid}`,
				[name, tel, province, city, county, addressDetail, isDefault, areaCode], (err, results) => {
					// console.log(results);
					if (err) { throw err }
					res.send({
						code: 200,
						msg: '修改成功',
						success: true
					})
				})
		}

	})
})

// 新增收货地址
router.post('/api/addadress', (req, res) => {
	let data = req.body;
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	// console.log(data);
	// console.log(tel);
	// 查找用户ID
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		if (err) throw err;
		// console.log(results);
		let uid = results[0].id;
		// 根据uid存收货地址
		let name = data.content.name;
		let tel = data.content.tel;
		let province = data.content.province;
		let city = data.content.city;
		let county = data.content.county;
		let addressDetail = data.content.addressDetail;
		let isDefault = data.content.isDefault;
		let areaCode = data.content.areaCode;
		if (isDefault != 1) {
			connection.query(`insert into address (uid,name,tel,province,city,county,addressDetail,isDefault,areaCode) values(${uid},"${name}","${tel}","${province}","${city}","${county}","${addressDetail}","${isDefault}","${areaCode}")`,
				(err, results) => {
					if (err) { throw err };
					console.log(results);
					res.send({
						code: 0,
						success: true,
					})

				})
		} else {
			// 查询出所有的地址  然后让所有的地址isDefault = 0
			connection.query(`update address set isDefault = 0 where uid = ${uid}`, (err, results) => {

				connection.query(`insert into address (uid,name,tel,province,city,county,addressDetail,isDefault,areaCode) values(${uid},"${name}","${tel}","${province}","${city}","${county}","${addressDetail}","${isDefault}","${areaCode}")`,
					(err, results) => {
						if (err) { throw err };
						console.log(results);
						res.send({
							code: 0,
							success: true,
						})

					})
			})

		}
	})

})


// 获取购物车数据
router.get('/api/cartData', (req, res) => {
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	console.log(tel);
	// console.log(tel);
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		if (err) { throw err }
		let uid = results[0].id
		connection.query('select * from goods_cart where uid = ?', uid, (err, results) => {
			if (err) { throw err }
			console.log(results);
			res.send({
				code: 200,
				success: true,
				data: {
					data: results
				}
			})
		})
	})
})

// 购物车添加商品
router.post('/api/addCart', (req, res) => {
	let token = req.headers.token;
	let user = jwt.decode(token)  //{ tel: '12345696354', iat: 1652270999 }
	let tel = user.tel;
	let goods_id = req.body.goodsId;
	console.log(req.body);
	// 查用户
	connection.query(`select * from user_list where tel = "${tel}"`, (err, results) => {
		if (err) { throw err }
		let uid = results[0].id;
		console.log(goods_id);

		// console.log(results);
		// 查商品
		connection.query(`select * from goods_list where id = "${goods_id}"`, (err, results) => {
			if (err) { throw err }
			// console.log(results);
			let goods_name = results[0].name;
			let goods_price = results[0].price;
			let goods_imgUrl = results[0].imgUrl;
			// 查询当前用户是否在这之前添加过商品
			connection.query(`select * from goods_cart where uid = ${uid} and goods_id = ${goods_id}`, (err, results) => {
				if (err) { throw err }
				// 说明之前添加过
				if (results.length > 0) {
					let goods_num = results[0].goods_num;
					let id = results[0].id;
					// 更新数据
					connection.query(`update goods_cart set goods_num = ${parseInt(goods_num) + 1} where id = ${id}`, (err, results) => {
						if (err) { throw err }
						res.send({
							code: 200,
							success: true,
							msg: '添加成功'
						})
					})
				} else {
					// 没添加过
					connection.query(`insert into goods_cart (uid,goods_id,goods_name,goods_price,goods_num,goods_imgUrl) values(${uid},${goods_id},"${goods_name}","${goods_price}",1,"${goods_imgUrl}")`,
						(err, results) => {
							if (err) { throw err }
							res.send({
								code: 0,
								success: true
							})
						})
				}
			})


		})
	})
})

// 修改密码
router.post('/api/recovery', (req, res) => {
	let tel = req.body.phone;
	let pwd = req.body.pwd;
	connection.query('update user_list set pwd = ? where tel = ?', [pwd, tel], (err, results) => {
		if (err) {
			throw err
		}
		res.send({
			code: 200,
			data: {
				success: true,
				msg: '修改成功'
			}
		})
	})
})

// 找回用户密码
router.post('/api/selectUser', (req, res) => {
	let tel = req.body.phone;
	connection.query('select * from user_list where tel = ?', tel, (err, results) => {
		if (err) {
			throw err
		}
		if (results.length > 0) {
			res.send({
				code: 0,
				data: {
					success: true
				}
			})
		} else {
			res.send({
				code: 0,
				data: {
					success: false,
					msg: '未找到该用户信息'
				}
			})
		}
	})
})

// 注册用户 1
router.post('/api/register', (req, res) => {
	let params = req.body;


	// 先查询用户是否存在
	connection.query('select * from user_list where tel = ?', params.userTel, (err, results) => {
		// 如果存在  直接返回用户信息  无视密码
		if (err) { throw err }
		if (results.length > 0) {
			res.send({
				code: 0,
				data: {
					success: true,
					msg: '登陆成功',
					data: results[0]
				}
			})
		} else {
			// 如果不存在 则添加用户   并且把用户返回给前端
			// 生成token
			let secret = 'xiaoluxian';
			let payload = { tel: params.userTel };
			let token = jwt.sign(payload, secret)
			connection.query('insert into user_list (tel,pwd,imgUrl,nickName,token) values(' + params.userTel + ',' + params.userPwd + ',"/images/user.jpeg","yuqing","' + token + '")', (err, results) => {
				if (err) { throw err }

				// 查询出数据返回给前端
				connection.query('select * from user_list where tel = ?', params.userTel, (err, results) => {
					if (err) { throw err }
					res.send({
						code: 200,
						data: {
							success: true,
							msg: '登陆成功',
							data: results[0]
						}
					})
				})
			})
		}
	})
})

// 增加一个用户 1
router.post('/api/addUser', (req, res) => {
	let tel = req.body.phone;
	// 查询是否有此用户
	connection.query('select * from user_list where tel = ?', tel, (err, results) => {
		if (err) { throw err }
		if (results.length > 0) {
			// 用户存在
			res.send({
				code: 200,
				data: {
					success: true,
					msg: "登陆成功",
					data: results[0]
				}
			})
		} else {
			// 生成token
			let secret = 'xiaoluxian';

			let payload = { tel: tel };
			let token = jwt.sign(payload, secret)
			// 不存在 则添加用户
			connection.query('insert into user_list (tel,pwd,imgUrl,nickName,token) values(' + tel + ',111111,"/images/user.jpeg","yuqing","' + token + '")', (err, results) => {
				if (err) { throw err }
				// 查询出数据返回给前端
				connection.query('select * from user_list where tel = ?', tel, (err, results) => {
					if (err) { throw err }
					res.send({
						code: 200,
						data: {
							success: true,
							msg: '登陆成功',
							data: results[0]
						}
					})
				})
			})
		}
	})
})

// 发送短信验证码
router.post('/api/code', (req, res) => {
	let tel = req.body.iphone;
	// res.send({tel})
	// 短信应用SDK AppID
	var appid = 1400187558;  // SDK AppID是1400开头

	// 短信应用SDK AppKey
	var appkey = "dc9dc3391896235ddc2325685047edc7";

	// 需要发送短信的手机号码
	var phoneNumbers = [tel];

	// 短信模板ID，需要在短信应用中申请
	var templateId = 285590;  // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请

	// 签名
	var smsSign = "羽乐购";  // NOTE: 这里的签名只是示例，请使用真实的已申请的签名, 签名参数使用的是`签名内容`，而不是`签名ID`

	// 实例化QcloudSms
	var qcloudsms = QcloudSms(appid, appkey);

	// 设置请求回调处理, 这里只是演示，用户需要自定义相应处理回调
	function callback(err, ress, resData) {
		if (err) {
			console.log("err: ", err);
		} else {
			res.send({
				code: 200,
				data: {
					success: true,
					data: ress.req.body.params[0]
				}
			})
			console.log("request data: ", ress.req);
			console.log("response data: ", resData);
		}
	}

	var ssender = qcloudsms.SmsSingleSender();
	var params = [Math.floor(Math.random() * (9999 - 1000)) + 1000];
	ssender.sendWithParam(86, phoneNumbers[0], templateId,
		params, smsSign, "", "", callback);  // 签名参数不能为空串
})

// 登录逻辑
router.post("/api/login", (req, res) => {
	let params = req.body;
	// console.log(params);
	// 先查找手机号  看是否有该用户
	connection.query('select * from user_list where tel = ?', params.userTel, (err, results) => {
		if (err) { console.log(err) }
		if (results.length > 0) {
			// 说明有
			// 看手机号对应的密码对不对  对的话   登陆成功
			connection.query('select * from user_list where tel = ? and pwd = ?', [params.userTel, params.userPwd], (err, results) => {
				if (results.length > 0) {
					res.send({
						code: 200,
						data: {
							success: true,
							msg: '登陆成功',
							data: results[0]
						}
					})
				} else {
					// 不对的话  密码错误
					res.send({
						code: 302,
						data: {
							success: false,
							msg: '密码错误'
						}
					})
				}
			})
		} else {
			res.send({
				code: 301,
				data: {
					success: false,
					msg: '手机号不存在'
				}
			})
		}
	})

})


// 查询商品数据接口
router.get('/api/goods/shopList', (req, res) => {
	// `select * from goods_list where name like %${searchName}%`
	// console.log(req.query);
	let [searchName, type] = Object.keys(req.query)
	let [searchVal, order] = Object.values(req.query)
	// let searchName = req.query.searchName;
	console.log(searchName, type, searchVal, order);
	if (type == 'zh') {
		connection.query('select * from goods_list where name like "%' + searchVal + '%"', (err, results) => {
			console.log(results);
			res.send({
				code: 0,
				data: results
			})
		})
	} else {
		connection.query('select * from goods_list where name like  "%' + searchVal + '%" order by ' + type + ' ' + order + '', (err, results) => {
			console.log(results);
			res.send({
				code: 0,
				data: results
			})
		})
	}
})

//首页推荐的数据
router.get('/api/index_list/0/data/1', function (req, res, next) {
	res.send({
		code: 0,
		data: {
			topBar: [
				{ id: 0, label: '推荐' },
				{ id: 1, label: '尤尼克斯' },
				{ id: 2, label: '李宁' },
				{ id: 3, label: '胜利' },

			],
			data: [
				//这是swiper
				{
					id: 0,
					type: 'swiperList',
					data: [
						{ id: 0, imgUrl: './images/badImages/yonex.jpg' },
						{ id: 1, imgUrl: './images/badImages/victory.jpg' },
						{ id: 3, imgUrl: './images/badImages/liningswiper.jpg' }
					]
				},
				//这是icons
				{
					id: 1,
					type: 'iconsList',
					data: [
						{
							id: 1,
							title: '球拍',
							imgUrl: './images/icons1.png'
						},
						{
							id: 2,
							title: '球鞋',
							imgUrl: './images/icons2.png'
						},
						{
							id: 3,
							title: '球线',
							imgUrl: './images/icons3.png'
						},
						{
							id: 4,
							title: '护具',
							imgUrl: './images/icons4.png'
						},
						{
							id: 5,
							title: '官方验证',
							imgUrl: './images/icons5.png'
						}
					]
				},
				//爆款推荐
				{
					id: 3,
					type: 'recommendList',
					data: [
						{
							id: 1,
							name: '尤尼克斯 Double10',
							content: '超细中杆 落点犀利',
							price: '1888',
							imgUrl: './images/badImages/d10.jpg'
						},
						{
							id: 2,
							name: '黑金极速10 李俊慧代言',
							content: '世界冠军首选 双打好拍',
							price: '1688',
							imgUrl: './images/badImages/js10BLACK.jpg'
						}
					]
				},
				//猜你喜欢
				{
					id: 4,
					type: 'likeList',
					data: [
						{
							id: 1,
							imgUrl: './images/badImages/9c.jpg',
							name: '李宁羽毛球拍 蓄力突袭9(3U/4U/5U) 进攻型 专业级单拍AYPR132',
							price: 1488
						},
						{
							id: 2,
							imgUrl: './images/badImages/90k.jpg',
							name: '正品victor胜利羽毛球拍单拍神速90 威克多专业速度进攻型ARS90S',
							price: 1488
						},
						{
							id: 3,
							imgUrl: './images/badImages/98k.jpg',
							name: '正品victor胜利羽毛球单拍 威克多速度高磅高端进攻拍神速ARS-98K',
							price: 1488
						},
						{
							id: 4,
							imgUrl: './images/badImages/99p.jpg',
							name: 'YONEX尤尼克斯羽毛球拍天斧AX99PRO空拍单框2021新款日版正品',
							price: 1488
						},
						{
							id: 5,
							imgUrl: './images/badImages/100zz.jpg',
							name: 'YONEX/尤尼克斯官网 天斧系列 ASTROX100GAME 全碳素轻量羽毛球拍',
							price: 1488
						},
						{
							id: 6,
							imgUrl: './images/badImages/arc11.jpg',
							name: '官网正品YONEX尤尼克斯羽毛球拍单拍全碳素超轻专业弓箭ARC11PRO',
							price: 1488
						},
						{
							id: 7,
							imgUrl: './images/badImages/fengying800.jpg',
							name: '【2022新品】李宁羽毛球单拍 锋影800 3U/4U 速度型双打 张楠同款',
							price: 1488
						},
						{
							id: 8,
							imgUrl: './images/badImages/gongfu.jpg',
							name: '2021威克多VICTOR胜利功夫DX-巭专业羽毛球拍碳纤维超轻攻守兼备',
							price: 1488
						},
						{
							id: 9,
							imgUrl: './images/badImages/guizhan.jpg',
							name: '真VICTOR威克多胜利TK鬼斩羽毛球拍全碳素纤维onigiri单拍进攻型',
							price: 1488
						},
						{
							id: 10,
							imgUrl: './images/badImages/n7.jpg',
							name: '正品LINING李宁能量聚合70N72白金色羽毛球拍轻量型单拍N7二代',
							price: 1488
						}
					]
				}

			]
		}
	})
});

// 尤尼克斯数据
router.get('/api/index_list/1/data/1', function (req, res, next) {
	res.send({
		code: 0,
		data: [
			{
				id: 1,
				type: 'adList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/yonex.jpg'
					},
					{
						id: 2,
						imgUrl: './images/badImages/lindan.jpg'
					}
				]
			},
			{
				id: 2,
				type: 'iconsList',
				data: [
					{
						id: 1,
						title: '球拍',
						imgUrl: './images/icons1.png'
					},
					{
						id: 2,
						title: '球鞋',
						imgUrl: './images/icons2.png'
					},
					{
						id: 3,
						title: '球线',
						imgUrl: './images/icons3.png'
					},
					{
						id: 4,
						title: '护具',
						imgUrl: './images/icons4.png'
					},
					{
						id: 5,
						title: '官方验证',
						imgUrl: './images/icons5.png'
					}
				]
			},
			{
				id: 3,
				type: 'likeList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/99p.jpg',
						name: 'YONEX尤尼克斯羽毛球拍天斧AX99PRO空拍单框2021新款日版正品',
						price: 1488
					},
					{
						id: 2,
						imgUrl: './images/badImages/100zz.jpg',
						name: 'YONEX/尤尼克斯官网 天斧系列 ASTROX100GAME 全碳素轻量羽毛球拍',
						price: 1488
					},
					{
						id: 3,
						imgUrl: './images/badImages/arc11.jpg',
						name: '官网正品YONEX尤尼克斯羽毛球拍单拍全碳素超轻专业弓箭ARC11PRO',
						price: 1488
					},
					{
						id: 4,
						imgUrl: './images/badImages/d10.jpg',
						name: '李宗伟奥运战拍 双刃10 正反异形框',
						price: 1488
					}
				]
			}

		]
	})
})


// 李宁数据
router.get('/api/index_list/2/data/1', function (req, res, next) {
	res.send({
		code: 0,
		data: [
			{
				id: 1,
				type: 'adList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/liningswiper.jpg'
					},
					{
						id: 2,
						imgUrl: './images/badImages/liningswiper.jpg'
					}
				]
			},
			{
				id: 2,
				type: 'likeList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/9c.jpg',
						name: '李宁羽毛球拍 蓄力突袭9(3U/4U/5U) 进攻型 专业级单拍AYPR132',
						price: 1299
					},
					{
						id: 2,
						imgUrl: './images/badImages/fengying800.jpg',
						name: '【2022新品】李宁羽毛球单拍 锋影800 3U/4U 速度型双打 张楠同款',
						price: 1299
					},
					{
						id: 3,
						imgUrl: './images/badImages/lining90mx.jpg',
						name: '现货 / 真拍李宁羽毛球拍雷霆90MAX趴龙腾套装限量专业进攻型单拍',
						price: 1299
					},
					{
						id: 3,
						imgUrl: './images/badImages/liningfengying500.jpg',
						name: '李宁LI-NING正品锋影500羽毛球拍全碳素3U进攻型4U均衡型单拍',
						price: 1299
					}
				]
			}

		]
	})
})


// 胜利数据
router.get('/api/index_list/3/data/1', function (req, res, next) {
	res.send({
		code: 0,
		data: [
			{
				id: 1,
				type: 'adList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/victory.jpg'
					},
					{
						id: 2,
						imgUrl: './images/badImages/victory.jpg'
					}
				]
			},
			{
				id: 2,
				type: 'likeList',
				data: [
					{
						id: 1,
						imgUrl: './images/badImages/90k.jpg',
						name: '正品victor胜利羽毛球拍单拍神速90 威克多专业速度进攻型ARS90S',
						price: 299
					},
					{
						id: 2,
						imgUrl: './images/badImages/98k.jpg',
						name: '正品victor胜利羽毛球单拍 威克多速度高磅高端进攻拍神速ARS-98K',
						price: 299
					},
					{
						id: 3,
						imgUrl: './images/badImages/gongfu.jpg',
						name: '2021威克多VICTOR胜利功夫DX-巭专业羽毛球拍碳纤维超轻攻守兼备',
						price: 299
					},
					{
						id: 4,
						imgUrl: './images/badImages/guizhan.jpg',
						name: '真VICTOR威克多胜利TK鬼斩羽毛球拍全碳素纤维onigiri单拍进攻型',
						price: 299
					}
				]
			}

		]
	})
})

// 商品列表数据
router.get('/api/goods/list', function (req, res, next) {

	res.send({
		code: 0,
		data: [
			{
				//一级
				id: 0,
				name: '推荐',
				data: [
					{
						//二级
						id: 0,
						name: '推荐',
						list: [
							//三级
							{
								id: 0,
								name: '突袭9c',
								imgUrl: './images/badImages/9c.jpg'
							},
							{
								id: 1,
								name: '风影800',
								imgUrl: './images/badImages/fengying800.jpg'
							},
							{
								id: 3,
								name: '天斧99',
								imgUrl: './images/badImages/99p.jpg'
							},
							{
								id: 4,
								name: '100zz',
								imgUrl: './images/badImages/100zz.jpg'
							},
							{
								id: 5,
								name: '双刃10',
								imgUrl: './images/badImages/d10.jpg'
							},
							{
								id: 6,
								name: 'js122',
								imgUrl: './images/badImages/js122.jpg'
							}
						]
					}
				]
			},
			{
				//一级
				id: 1,
				name: '专业级',
				data: [
					{
						//二级
						id: 0,
						name: '专业级',
						list: [
							//三级
							{
								id: 0,
								name: '突袭9c',
								imgUrl: './images/badImages/9c.jpg'
							},
							{
								id: 1,
								name: '风影800',
								imgUrl: './images/badImages/fengying800.jpg'
							},
							{
								id: 3,
								name: '天斧99',
								imgUrl: './images/badImages/99p.jpg'
							},
							{
								id: 4,
								name: '100zz',
								imgUrl: './images/badImages/100zz.jpg'
							},
							{
								id: 5,
								name: '双刃10',
								imgUrl: './images/badImages/d10.jpg'
							},
							{
								id: 6,
								name: 'js122',
								imgUrl: './images/badImages/js122.jpg'
							}
						]
					}
				]
			},
			{
				//一级
				id: 2,
				name: '高端',
				data: [
					{
						//二级
						id: 0,
						name: '高端',
						list: [
							//三级
							{
								id: 0,
								name: '突袭9c',
								imgUrl: './images/badImages/9c.jpg'
							},
							{
								id: 1,
								name: '风影800',
								imgUrl: './images/badImages/fengying800.jpg'
							},
							{
								id: 3,
								name: '天斧99',
								imgUrl: './images/badImages/99p.jpg'
							},
							{
								id: 4,
								name: '100zz',
								imgUrl: './images/badImages/100zz.jpg'
							},
							{
								id: 5,
								name: '双刃10',
								imgUrl: './images/badImages/d10.jpg'
							},
							{
								id: 6,
								name: 'js122',
								imgUrl: './images/badImages/js122.jpg'
							}
						]
					}
				]
			},
			{
				//一级
				id: 3,
				name: '中端',
				data: [
					{
						//二级
						id: 0,
						name: '中端',
						list: [
							//三级
							{
								id: 0,
								name: '突袭9c',
								imgUrl: './images/badImages/9c.jpg'
							},
							{
								id: 1,
								name: '风影800',
								imgUrl: './images/badImages/fengying800.jpg'
							},
							{
								id: 3,
								name: '天斧99',
								imgUrl: './images/badImages/99p.jpg'
							},
							{
								id: 4,
								name: '100zz',
								imgUrl: './images/badImages/100zz.jpg'
							},
							{
								id: 5,
								name: '双刃10',
								imgUrl: './images/badImages/d10.jpg'
							},
							{
								id: 6,
								name: 'js122',
								imgUrl: './images/badImages/js122.jpg'
							}
						]
					}
				]
			},
			{
				//一级
				id: 4,
				name: '低端',
				data: [
					{
						//二级
						id: 0,
						name: '低端',
						list: [
							//三级
							{
								id: 0,
								name: '突袭9c',
								imgUrl: './images/badImages/9c.jpg'
							},
							{
								id: 1,
								name: '风影800',
								imgUrl: './images/badImages/fengying800.jpg'
							},
							{
								id: 3,
								name: '天斧99',
								imgUrl: './images/badImages/99p.jpg'
							},
							{
								id: 4,
								name: '100zz',
								imgUrl: './images/badImages/100zz.jpg'
							},
							{
								id: 5,
								name: '双刃10',
								imgUrl: './images/badImages/d10.jpg'
							},
							{
								id: 6,
								name: 'js122',
								imgUrl: './images/badImages/js122.jpg'
							}
						]
					}
				]
			},
			{
				//一级
				id: 5,
				name: '球线',
				data: [
					{
						//二级
						id: 0,
						name: '球线',
						list: [
							//三级
							{
								id: 0,
								name: '66u',
								imgUrl: './images/badImages/66u.jpg'
							},
							{
								id: 1,
								name: '80线',
								imgUrl:'./images/badImages/80线.jpg'
							},
							{
								id: 2,
								name: '95线',
								imgUrl: './images/badImages/95线.jpg'
							},
							{
								id: 3,
								name: '98线',
								imgUrl: './images/badImages/98线.jpg'
							},
							{
								id: 4,
								name: 'ab线',
								imgUrl: './images/badImages/ab线.jpg'
							},
							{
								id: 5,
								name: '99线',
								imgUrl: './images/badImages/99线.jpg'
							}
						]
					}
				]
			}

		]
	})
})

// 根据ID查询ID的接口
router.get('/api/goods/:id', function (req, res, next) {
	let id = req.params.id
	console.log(id);
	connection.query('select * from goods_list where id = ?', id, (err, results) => {
		if (err) { return err }
		res.send({
			code: 0,
			data: results
		})
	})
})
module.exports = router;
